define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            var params = '/goods_id/' + Config.goods_id + '/cat_ids/' + Config.cat_ids;
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mall/goods/salespec/index' + params,
                    add_url: 'mall/goods/salespec/add' + params,
                    edit_url: 'mall/goods/salespec/edit' + params,
                    //del_url: 'mall/goods/salespec/del' + params,
                    multi_url: 'mall/goods/salespec/multi' + params,
                    table: 'mall_goods_salespec',
                }
            });

            var table = $("#table");

            //设置字段
            var columns = [
                {checkbox: true},
                {field: 'id', title: __('ID')},
                {field: 'goods_id', title: __('Goods_id'), operate: "in", formatter: Table.api.formatter.search, visible:false},
            ];
            //动态追加分类规格作为字段
            $.each(Config.specs, function (i, j) {
                var data = {field: "specid_" + i, title: j.specname, operate: 'like'};
                columns.push(data);
            });
            //追加表后续字段
            columns.push({field: 'specitem_ids', title: __('Specitem_ids'), visible:false});
            columns.push(
                {field: 'productno', title: __('Productno')}
            );
            columns.push(
                {field: 'marketprice', title: __('Marketprice'), operate:'BETWEEN', editable: true}
            );
            columns.push(
                {field: 'specprice', title: __('Specprice'), operate:'BETWEEN', editable: true}
            );
            columns.push(
                {field: 'specstock', title: __('Specstock'), editable: true}
            );
            columns.push(
                {field: 'warnstock', title: __('Warnstock'), editable: true}
            );
            columns.push(
                {field: 'salenum', title: __('Salenum'), visible:false}
            );
            columns.push(
                {field: 'defaultis', title: __('Defaultis'), searchList: {"1":__('Yes'),"0":__('No')}, formatter: Table.api.formatter.toggle}
            );
            columns.push(
                {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
            );

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'asc',
                columns: columns,
                //pagination: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on('click', '.btn-start', function () {
                var that = this;
                var msg = Config.buildSaleSpecIs?"重新生成销售规格会删除原来的数据，确定码？":'你确定要生成销售规格吗?';
                Layer.confirm(msg, function () {
                    Fast.api.ajax($.fn.bootstrapTable.defaults.extend.add_url, function () {
                        Layer.closeAll();
                        table.bootstrapTable('refresh');
                    }, function () {
                        Layer.closeAll();
                    });
                });
                return false;
            });
        },
        add: function () {
            //Controller.api.bindevent();
            Table.api.bindevent(table);
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            events: {//绑定事件的方法
                save: {
                    //格式为：方法名+空格+DOM元素
                    'click .btn-save': function (e, value, row, index) {
                        e.stopPropagation();
                        e.preventDefault();
                        var table = $(this).closest('table');
                        var options = table.bootstrapTable('getOptions');
                        //var ids = row[options.pk];
                        row = $.extend({}, row ? row : {});
                        var url = options.extend.add_url;
                        Fast.api.open(Table.api.replaceurl(url, row, table), __('Add'), $(this).data() || {});
                    }
                },
            }

        }
    };
    return Controller;
});